package edu.mobbuzz.main;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

import edu.mobbuzz.messaging.Messaging;


public class Schedular {
	private Messaging messaging;
	
	public Messaging getMessaging() {
		return messaging;
	}

	public void setMessaging(Messaging messaging) {
		this.messaging = messaging;
	}


	public void run() throws Exception {
		Log log = LogFactory.getLog(Schedular.class);

        log.info("------- Initializing -------------------");

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.info("------- Initialization Complete --------");

        log.info("------- Scheduling Jobs ----------------");
        
        // get a "nice round" time a few seconds in the future...
        long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();

        // job1 will only fire once at date/time "ts"
        JobDetail job = new JobDetail("job1", "group1", RssUpdater.class);
        job.getJobDataMap().put("messaging", messaging);
        SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 7000L);
        
        // schedule it to run!
        Date ft = sched.scheduleJob(job, trigger);
        sched.start();
        
	}
	
	
	public static void main(String[] args) throws Exception{
		Schedular _schedular = new Schedular();
		_schedular.run();
	}
}
